<template>
  <van-icon
    @click="oncollect"
    :color="value?'red':''"
    :name="value? 'star':'star-o'"
  />
</template>

<script>
import { Toast } from 'vant'
import { collectarticle, cancelcollect } from '@/api/article'
export default {
  data () {
    return {

    }
  },
  props: {
    value: {
      type: Boolean,
      required: true
    },
    art_id: {
      type: [Number, Object, String],
      required: true
    }

  },

  created () {

  },

  methods: {
    async oncollect () {
      Toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true,
        message: '加载中'
      })
      try {
        if (this.value) {
          console.log('取消收藏')
          await cancelcollect(this.art_id)
        } else {
          await collectarticle(this.art_id)
          console.log('收藏')
        }
        Toast.success('操作成功')
        this.$emit('input', !this.value)
      } catch (error) {
        console.log(error)
        Toast.fail('操作失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
